import pymysql

db = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', database='test_123', charset='utf8')

cur = db.cursor()
with open('mysql.jpg', 'rb') as fd:
    data = fd.read()
try:
    sql = "insert into Images values \
           (1,'mysql.jpg',%s)"
    cur.execute(sql, [data])
    db.commit()
except Exception as e:
    db.rollback()
    print(e)

cur.close()
db.close()
